package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Transactional
    public void addDish(DishDTO dishDTO) {
        // 添加菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.addDish(dish);

        // 添加菜品口味
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size()>0) {
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            dishFlavorMapper.insertDishFlavorBatch(flavors);
        }
    }


    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Transactional
    public void delDish(List<Long> ids) {
        // 起售中的菜品不能删除
        Integer countSale = dishMapper.countDishOnSale(ids);

        if (countSale>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        // 关联了套餐的菜品不能删除
        Integer countSetmeal = setmealDishMapper.countSetmealByDishId(ids);
        if (countSetmeal>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        // 删除菜品
        dishMapper.delDishBatch(ids);

        // 删除菜品口味
        dishFlavorMapper.delFlavorsBatch(ids);
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        // 复制属性
        BeanUtils.copyProperties(dishDTO,dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(flavor -> flavor.setDishId(dishDTO.getId()));

        // 修改菜品信息
        dishMapper.updateDish(dish);
        // 删除菜品的口味信息
        dishFlavorMapper.delFlavorsBatch(Arrays.asList(dishDTO.getId()));

        // 添加菜品的口味信息
        if (flavors.size()>0) {
            dishFlavorMapper.insertDishFlavorBatch(flavors);
        }

    }

    /**
     * 起售、停售菜品
     *
     * @param status
     * @param id
     */
    public void updateDishStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.updateDishStatus(dish);

        // 一旦菜品停售，所关联的套餐一起停售
        if (status == 0) {
            List<Long> setmeals = setmealDishMapper.getSetmealsByDishId (id);
            for (Long setmealId : setmeals) {
                setmealMapper.updateSetmealStatus(Setmeal.builder().status(0).id(setmealId).build());
            }
        }

    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page<DishVO> p = dishMapper.pageDish(dishPageQueryDTO);

        return new PageResult(p.getTotal(),p.getResult());
    }

    /**
     * 根据id查询菜品信息
     *
     * @param id
     * @return
     */
    public DishVO getDishById(Long id) {
        DishVO dishVO = dishMapper.getDishById(id);
        return dishVO;
    }

    /**
     * 根据分类id查询菜品信息
     *
     * @param categoryId
     * @return
     */
    public List<Dish> getDishByCategoryId(Long categoryId) {
        List<Dish> dish = dishMapper.getDishByCategoryId(categoryId);
        return dish;
    }

    /**
     * C端-根据分类id浏览在售中的菜品
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> list = dishMapper.getDishByCategoryIdAndStatus(dish);
        return list;
    }
}
